package com.jcnet.bizaia;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import android.app.ActionBar;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.TextView;

import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.MenuItem;
import com.jcnet.bizaia.bean.BusinessCategoryBean;
import com.jcnet.bizaia.bean.CatgoryBean;
import com.jcnet.bizaia.bean.SearchBean;
import com.jcnet.bizaia.bean.VodSearchInfoBean;
import com.jcnet.bizaia.commCategory.BusinessCategoryList;
import com.jcnet.bizaia.httpRequest.CategoryHttpRequstTools;
import com.loopj.android.http.RequestParams;

public class VodSearchActivity extends JCSimpleSherlockActivity implements OnClickListener{

	private BroadcastReceiver broadcastReceiver;
	
	public static final String RECEIVER_VODSEARCHACTIVITY = "RECEIVER_VODSEARCHACTIVITY";
	
	private int mark = -1;
	
	private VodSearchInfoBean vodSearchInfoBean;
	
	private EditText EditText_Search;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.vod_search);
		
		EditText_Search = (EditText)findViewById(R.id.EditText_Search);
		
		vodSearchInfoBean = new VodSearchInfoBean(null,-1,-1);
		
		super.jcTitle = getString(R.string.vodSearch);
		
		super.initActionBar();
		
		broadcastReceiver = new BroadcastReceiver() {
			@Override
			public void onReceive(Context context, Intent intent) {
				// TODO Auto-generated method stub
				if(intent.getAction()==RECEIVER_VODSEARCHACTIVITY){
					List<SearchBean> searchBeans = new ArrayList<SearchBean>();   //传递给下个页面的数据
					List<BusinessCategoryBean> catgoryBeans = (List<BusinessCategoryBean>) intent.getExtras().getSerializable("AllBusinessCategoryLisyBean");
					if(catgoryBeans!=null){
						for (BusinessCategoryBean catgoryBean : catgoryBeans) {
							searchBeans.add(new SearchBean(catgoryBean.getIndustryname(), catgoryBean.getIid()));
						}
					}
					
					Intent intents =new Intent(context, SearchListActivity.class);
					intents.putExtra("listVa", (Serializable)searchBeans);
					startActivityForResult(intents, Intent.EXTRA_DOCK_STATE_CAR);
					
					System.out.println("come on baby!!!!!!!!");
				}
				if(intent.getAction()==DiscussSearchActivity.DISS_BISNUESS_RECEIVER){
					List<SearchBean> searchBeans = new ArrayList<SearchBean>();   //传递给下个页面的数据
					List<CatgoryBean> catgoryBeans = (List<CatgoryBean>) intent.getExtras().getSerializable("catgoryBeans");
					if(catgoryBeans!=null){
						for (CatgoryBean catgoryBean : catgoryBeans) {
							searchBeans.add(new SearchBean(catgoryBean.getCname(), catgoryBean.getCid()));
						}
					}
					
					Intent intents =new Intent(context, SearchListActivity.class);
					intents.putExtra("listVa", (Serializable)searchBeans);
					startActivityForResult(intents, Intent.EXTRA_DOCK_STATE_CAR);
				}
				
				
				
			}
		};
		
		IntentFilter intentFilter = new IntentFilter();
		intentFilter.addAction(RECEIVER_VODSEARCHACTIVITY);
		intentFilter.addAction(DiscussSearchActivity.DISS_BISNUESS_RECEIVER);
		
		registerReceiver(broadcastReceiver, intentFilter);
	}


	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
		System.out.println("1111111111");
		if(resultCode==DiscussSearchActivity.RESULT_CODE){

			System.out.println("??????????????????");
			
			SearchBean searchBean = (SearchBean) data.getExtras().getSerializable("resultBean");
			switch (mark) {
			case 1:
				
				((TextView)findViewById(R.id.vodBusinessCategory)).setText(searchBean.getSearchName());
				
				vodSearchInfoBean.setVodSearchBusinessCategoryID(searchBean.getSearchID());
				
				break;
			case 2:
				((TextView)findViewById(R.id.vodCategory)).setText(searchBean.getSearchName());
				
				vodSearchInfoBean.setVodSearchCategoryID(searchBean.getSearchID());
				break;
			

			default:
				break;
			}
			
		}
	}
	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		switch (v.getId()) {
		case R.id.vodBusinessLin:      //所有行业分类
			mark = 1;
			RequestParams params = new RequestParams();
			params.put("languageNum", App.localeCode);
			params.put("midNum", 1);  
			new  BusinessCategoryList(VodSearchActivity.this, params).getAllCategory();
			break;

		case R.id.vodCategoryLin:      //请求所有分类
			mark=2;
			
			RequestParams params2 = new RequestParams();
			params2.put("languageNum", App.localeCode);
			params2.put("midNum", 2);   //所属菜单
			new  CategoryHttpRequstTools(VodSearchActivity.this, params2).getAllCategory();
			
			break;
		case R.id.vodSearchButton:
			
			
			//跳转到 搜索页面
			

			vodSearchInfoBean.setVodSearchTitle(EditText_Search.getText().toString().trim());
			
			
			
			Intent intent2 = new Intent(VodSearchActivity.this, SearchResultActivity.class);
			intent2.putExtra("vodSearchItem", vodSearchInfoBean);
			startActivity(intent2);

			break;

		case R.id.vodClearSearchButton:

			vodSearchInfoBean = new VodSearchInfoBean(null, -1, -1);
			
			((TextView)findViewById(R.id.vodBusinessCategory)).setText(getString(R.string.allBusinessCategory));
			
			((TextView)findViewById(R.id.vodCategory)).setText(getString(R.string.allCategory));
			
			EditText_Search.setText(" ");
			
			
			

			break;

		default:
			break;
		}
	}
	
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		switch (item.getItemId()) {
		case android.R.id.home:
			finish();
			break;

		default:
			break;
		}
		
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		
		unregisterReceiver(broadcastReceiver);
	}
	
	
}
